package com.iwomy.secureplat.platform.service.impl;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.iwomy.secureplat.common.data.datascope.DataScope;
import com.iwomy.secureplat.common.security.util.SecurityUtils;
import com.iwomy.secureplat.platform.dto.BasicDeviceTypeDTO;
import com.iwomy.secureplat.platform.entity.*;
import com.iwomy.secureplat.platform.mapper.BasicAuthorizationRecordMapper;
import com.iwomy.secureplat.platform.mapper.BasicDeviceModelMapper;
import com.iwomy.secureplat.platform.mapper.BasicDeviceTypeMapper;
import com.iwomy.secureplat.platform.service.BasicDeviceTypeService;
import com.iwomy.secureplat.platform.utils.RemoteDeptUtils;
import com.iwomy.secureplat.platform.vo.BasicDeviceModelVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 设备类型
 *
 * @author pig
 * @date 2023-11-14 10:07:32
 */
@Service
public class BasicDeviceTypeServiceImpl extends ServiceImpl<BasicDeviceTypeMapper, BasicDeviceTypeEntity> implements BasicDeviceTypeService {

	@Autowired
	private BasicAuthorizationRecordMapper authorizationRecord;

	@Autowired
	private BasicDeviceModelMapper basicDeviceModel;

	@Autowired
	private RemoteDeptUtils remoteDeptUtils;

	@Override
	public Page getBasicDeviceTypePage(Page page, BasicDeviceTypeDTO basicDeviceTypeDTO) {
		LambdaQueryWrapper<BasicDeviceTypeEntity> queryWrapper = Wrappers.lambdaQuery();
		List<BasicDeviceTypeEntity> list = this.baseMapper.selectListByScope(queryWrapper, new DataScope());
		List<Long> deviceTypeId = list.stream().map(BasicDeviceTypeEntity::getId).collect(Collectors.toList());

		if (ObjectUtil.isEmpty(deviceTypeId)) return page;

		LambdaQueryWrapper<BasicDeviceTypeEntity> wrapper = Wrappers.lambdaQuery();

		if (StrUtil.isNotEmpty(basicDeviceTypeDTO.getTypeName())) {
			wrapper.like(BasicDeviceTypeEntity::getTypeName, basicDeviceTypeDTO.getTypeName());
		}
		if (StrUtil.isNotEmpty(basicDeviceTypeDTO.getProtocolName())) {
			wrapper.like(BasicDeviceTypeEntity::getProtocolName, basicDeviceTypeDTO.getProtocolName());
		}

		if (ArrayUtil.isNotEmpty(basicDeviceTypeDTO.getCreateTime())) {
			wrapper.ge(BasicDeviceTypeEntity::getCreateTime, basicDeviceTypeDTO.getCreateTime()[0]).le(BasicDeviceTypeEntity::getCreateTime,
					basicDeviceTypeDTO.getCreateTime()[1]);
		}

		wrapper.in(BasicDeviceTypeEntity::getId, deviceTypeId);

		return baseMapper.selectPage(page, wrapper);
	}

	@Override
	public List<? extends Object> pageByScope(BasicDeviceTypeEntity basicDeviceTypeEntity) {
		Map<String, Object> data = remoteDeptUtils.getDept(SecurityUtils.getUser().getDeptId());
		String parentIdString = (String) data.getOrDefault("parentId", null);
		Long parentId = Long.parseLong(parentIdString);

		List<BasicDeviceTypeEntity> basicDeviceType = new ArrayList<>();
		//是租户时，产品库为设备列表
		if (parentId == 0) {
			LambdaQueryWrapper<BasicDeviceTypeEntity> queryWrapper = Wrappers.lambdaQuery();
			queryWrapper.eq(BasicDeviceTypeEntity::getState, 10);
			basicDeviceType = this.getBaseMapper().selectListByScope(queryWrapper, new DataScope());
		}

		LambdaQueryWrapper<BasicAuthorizationRecordEntity> queryWrapper = Wrappers.lambdaQuery();
		queryWrapper.in(BasicAuthorizationRecordEntity::getDeptId, SecurityUtils.getUser().getDeptId());
		List<BasicAuthorizationRecordEntity> authorizationRecordList = authorizationRecord.selectList(queryWrapper);
		List<Long> deviceModelId = authorizationRecordList.stream().map(BasicAuthorizationRecordEntity::getDeviceModelId).collect(Collectors.toList());
		List<BasicDeviceModelVO> resultList = new ArrayList<>();
		if (deviceModelId.size() != 0) {
			//多表查询
			MPJLambdaWrapper<BasicDeviceModelEntity> wrapper = new MPJLambdaWrapper<>();
			wrapper.selectAll(BasicDeviceModelEntity.class)
					.selectAs(BasicDeviceTypeEntity::getTypeName, BasicDeviceModelVO::getTypeName)
					.leftJoin(BasicDeviceTypeEntity.class, BasicDeviceTypeEntity::getId, BasicDeviceModelEntity::getDeviceTypeId)
					.eq(BasicDeviceModelEntity::getState, 10)
					.in(BasicDeviceModelEntity::getId, deviceModelId);

			List<BasicDeviceModelVO> records = basicDeviceModel.selectJoinList(BasicDeviceModelVO.class, wrapper);

			// 设备类型去重，并转换为BasicDeviceModelVO的对象
			Map<String, BasicDeviceModelVO> deviceTypeMap = new HashMap<>();
			for (BasicDeviceModelVO record : records) {
				String deviceType = record.getTypeName();
				if (!deviceTypeMap.containsKey(deviceType)) {
					BasicDeviceModelVO deviceTypeVO = new BasicDeviceModelVO();
					BeanUtils.copyProperties(record, deviceTypeVO);
					deviceTypeVO.setId(record.getDeviceTypeId());
					deviceTypeMap.put(deviceType, deviceTypeVO);
				}
			}
			// 获取去重后的结果列表
			resultList = new ArrayList<>(deviceTypeMap.values());

		}
		if (resultList.isEmpty()) {
			return basicDeviceType;
		}

		List<Object> combinedList = Stream.of(basicDeviceType, resultList)
				.flatMap(Collection::stream)
				.collect(Collectors.toList());

		return combinedList;
	}

	@Override
	public List<BasicDeviceTypeEntity> listByScope(LambdaQueryWrapper<BasicDeviceTypeEntity> lambdaQueryWrapper, DataScope dataScope) {
		List<BasicDeviceTypeEntity> list = this.getBaseMapper().selectListByScope(lambdaQueryWrapper, dataScope);
		return list;
	}

}